package org.nokatag.system;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nokatag.jdom.JDOMException;

public class FileManager {
	public static String fileidToFileNames(String fid,String fileids,Integer showtype,String s,String e,String basepath) throws FileNotFoundException, JDOMException, IOException{
		StringBuilder bodystr = new StringBuilder();
		Long[] fileidlongs =  Function.stringTolongArry(fileids,",");
		for(Long fileid:fileidlongs){
			String filep=path(Concest.FILESERVERPATH,Concest.UP_FILEID,String.valueOf(fileid));//文件路径
			Map<String,String> filehead=getFileHead(filep);
			switch(showtype){
			case 1:bodystr.append(s+"<a target=\"_black\"  title=\""+filehead.get(Concest.UP_FILEFTEXT)+"\" href='"+basepath+"/nokatag/nokatag_uploadfile/"+fid+"?do=get&fileid="+String.valueOf(fileid)+"'>"+filehead.get(Concest.UP_FILENAME)+"</a>"+e+"\n");
			break;
			case 2:bodystr.append(s+"<a target=\"_black\"  title=\""+filehead.get(Concest.UP_FILEFTEXT)+"\" href='"+basepath+"/nokatag/nokatag_uploadfile/"+fid+"?do=red&fileid="+String.valueOf(fileid)+"'>"+filehead.get(Concest.UP_FILENAME)+"</a>"+e+"\n");
			break;
			case 0:bodystr.append(s+filehead.get(Concest.UP_FILENAME)+e+"\n");
			break;
			case 3:bodystr.append(basepath+"/nokatag/nokatag_uploadfile/"+fid+"?do=red&fileid="+String.valueOf(fileid)+"&ft="+filehead.get(Concest.UP_FILEFTEXT)+"\n");
			break;
			}
		}
		return bodystr.toString();
	}
	//----------------找出目录下的所有文件----------------------
	public static List<String> FileList(String filepath){
		 filepath=filepath.replace("\\", "/");
		 if(filepath==null){
			return null;
		 }
		 File root = new File(filepath);
	     File[] filesOrDirs = root.listFiles();
	     List<String> filelist=new ArrayList<String>();
	     if(filesOrDirs!=null){
	     for (int i = 0; i < filesOrDirs.length; i++){
	            if (filesOrDirs[i].isDirectory()){
	            	filelist.addAll(FileList(filesOrDirs[i].getAbsolutePath()));
	            } else{
	                filelist.add(filepath+"/"+filesOrDirs[i].getName());

	            }
	        }
	     }else{
	    	 return null;
	     }
	     return filelist;
	}
	//-----------------找出合符ID的文件----------------------
	public static String path(String path,String fild,String value){
		List<String> fl=FileManager.FileList(path);
		if(fl==null){
			return null;
		}
		for(String f:fl){
			try{
				FileReader fr=new FileReader(f);
				BufferedReader br=new BufferedReader(fr);
				for(int i=0;i<Concest.UP_FILEFILEDSUN;i++){
					String fav= br.readLine();
					if(fav!=null && fav.indexOf(":")!=-1){
						String filed=fav.substring(0,fav.indexOf(":"));
						String valu=fav.substring(fav.indexOf(":")+1,fav.length());
						BugInfoWrint.Print("原始=== 字段："+filed+"  值："+valu+"   查找=== 字段："+fild+" 值:"+value);
						if(filed.equals(fild) && valu.equals(value)){
							return f.replace("\\", "/");
						}
					}
				}
				fr.close();
				br.close();
			}catch(Exception se){
				BugInfoWrint.Print(se);
			}
		}
		return null;
	}
	//--------------------找出文件头的偏移量-------------------
	public static Long fileSkin(String path){
		Long skin=0L;
		try{
			FileReader fr=new FileReader(path);
			BufferedReader br=new BufferedReader(fr);
			for(int i=0;i<Concest.UP_FILEFILEDSUN;i++){
				String body = br.readLine();
				if(body!=null)
					skin+=body.getBytes().length+2;
			}
			fr.close();
			br.close();
			return skin;
		}catch(Exception se){
			BugInfoWrint.Print(se);
			return null;
		}
	}
	//--------------------找单个文件头------------------------
	public static Map<String,String>getFileHead(String path){
		Map<String,String> fMap=new HashMap<String,String>();
		try{
			FileReader fr=new FileReader(path);
			BufferedReader br=new BufferedReader(fr);
			for(int i=0;i<Concest.UP_FILEFILEDSUN;i++){
				String fav= br.readLine();
				if(fav!=null){
					if(fav.indexOf(":")!=-1){
						String filed=fav.substring(0,fav.indexOf(":"));
						String valu=fav.substring(fav.indexOf(":")+1,fav.length());
						fMap.put(filed, valu);
					}
				}
			}
			fr.close();
			br.close();
			return fMap;
		}catch(Exception se){
			BugInfoWrint.Print(se);
			return null;
		}
		
	}
	//-------------------------找出文件头---------------------
	public static List<Map<String,String>> fileHead(String path,String filetype){
		List<String> fl=FileManager.FileList(path);
		if(fl==null){
			return null;
		}
		List<Map<String,String>> FHList=new ArrayList<Map<String,String>>();
		for(String f:fl){
			try{
				FileReader fr=new FileReader(f);
				BufferedReader br=new BufferedReader(fr);
				Map<String,String> fMap=new HashMap<String,String>();
				Boolean isadd=false;
				for(int i=0;i<Concest.UP_FILEFILEDSUN;i++){
					String fav= br.readLine();
					if(fav!=null){
						if(fav.indexOf(":")!=-1){
							String filed=fav.substring(0,fav.indexOf(":"));
							String valu=fav.substring(fav.indexOf(":")+1,fav.length());
							fMap.put(filed, valu);
							BugInfoWrint.Print("原始=== 字段："+filed+"  值："+valu+"   查找=== 字段："+filetype);
							if(filetype.equalsIgnoreCase("*")){
								isadd=true;
							}else if(filed.equals("filetype") && (filetype.trim().indexOf("*."+valu)!=-1)){//*.txt
								isadd=true;
							}
						}
					}
				}
				fr.close();
				br.close();
				if(isadd){
					FHList.add(fMap);
				}
			}catch(Exception se){
				BugInfoWrint.Print(se);
				return null;
			}
		}
		return FHList;
	}
	//-------------------生成临时文件---------------------------
	public static void main(String[] args){
		String filepath="C:/soceft/Tomcat60/webapps/nokatag/fileserver/5166468958484/gradient-bg.ncks";
		File filedel = new File(filepath);
		File filedel2 = new File(filepath.substring(0,filepath.lastIndexOf("/")));
		filedel.deleteOnExit();
		System.out.println("ffffffffffff:"+filedel.delete());
		filedel2.delete();
		filedel2.deleteOnExit();
	}

}
